					**This file runs late balanced event study**	

			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**


use removalcounts, clear


**
*Make indicator variables
**


gen centeredmonth = month-policymonth

replace centeredmonth = 0 if policymonth == .

*Indicator variables for each centered month variable (only 5 years before and after)
tab centeredmonth if centeredmonth >= -10 & centeredmonth <= 10, gen(indcentered)

* Rename these 
forv j = 1/10{
	loc k = abs(`j' - 11)
	rename indcentered`j' indcenteredN`k'
}
forv j = 11/21{
	loc k = `j' - 11
	rename indcentered`j' indcentered`k'
}

*Now set first indicator to include all previous obs and last to include all subsequent
replace indcenteredN10 = 1 if centeredmonth <= -10
replace indcentered10 = 1 if centeredmonth >= 10 

* Change order of indcenteredN0 (for which y - y* = 0) so that is omitted category
order indcentered0, last

*Replace missing months with zeros 
foreach var of varlist ind* {
replace `var' = 0 if `var' != 1
}

**
*Keep Late Panel Only


keep if firstmonth <= m(2012m8)
keep if month >= m(2012m8) 

*Ten month window on either side of treatment
gen ten = 1 if centeredmonth == 10 | centeredmonth == -10

egen ten_window = sum(ten), by(statecounty)

keep if ten_window == 2

count
assert r(N) == 5084

***
*Run regressions and produce figures
***

**
* All counts: 124 counties, 5084 observations, 76693 deportations
**


**
* All counts
**

glm countymonthlytotal  i.statecounty i.month indcenteredN10-indcentered10 , family(nbinomial ml) ///
cluster(state) link(log) irls

preserve

* Get coef
regsave indcenteredN10-indcentered10, detail(all)

* Create variables
gen ub = coef + 1.96   * stderr  * sqrt(deviance/df)
gen lb = coef - 1.96 * stderr  * sqrt(deviance/df)


* Generate time variable and other graphing stuff
gen time = _n-11
replace time = time+1 if time > -1

twoway (rspike ub lb time , lw(0.3) lc(gray)) (scatter coef time , msize(small) msymbol(circle) ), ///
	yli(0, lpattern(dash) lc(black)) ///
	ylab(-1.6(.2)0.6, labsize(small) angle(horizontal)) xlab(-10(2)10, labsize(small)) ///
	title("All") ///
	ytitle("") xtitle("") ///
	graphregion( fcolor(white) lcolor(white)) plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none)) ///
	legend(off) xsize(5) ysize(3) name(a, replace)

restore

**
* Threat 1 counts
**

glm threat1count indcenteredN10-indcentered10 i.month i.statecounty  , family(nbinomial ml) ///
cluster(state) link(log) irls

preserve

* Get coef
regsave indcenteredN10-indcentered10, detail(all)

* Create variables
gen ub = coef + 1.96   * stderr  * sqrt(deviance/df)
gen lb = coef - 1.96 * stderr  * sqrt(deviance/df)

* Generate time variable and other graphing stuff
gen time = _n-11
replace time = time+1 if time > -1


twoway (rspike ub lb time , lw(0.3) lc(gray)) (scatter coef time , msize(small) msymbol(circle) ), ///
	yli(0, lpattern(dash) lc(black)) ///
	ylab(-1.6(.2)0.6, labsize(small) angle(horizontal)) xlab(-10(2)10, labsize(small)) ///
	title("People with Category 1 Convictions") ///
	ytitle("") xtitle("") ///
	graphregion( fcolor(white) lcolor(white)) plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none)) ///
	legend(off) xsize(5) ysize(3) name(b, replace)

restore




**
* Threat 0 counts
**


glm threat0count indcenteredN10-indcentered10 i.month i.statecounty  , family(nbinomial ml) ///
cluster(state) link(log) irls

preserve

* Get coef
regsave indcenteredN10-indcentered10, detail(all)

* Create variables
gen ub = coef + 1.96   * stderr  * sqrt(deviance/df)
gen lb = coef - 1.96 * stderr  * sqrt(deviance/df)

* Generate time variable and other graphing stuff
gen time = _n-11
replace time = time+1 if time > -1


twoway (rspike ub lb time , lw(0.3) lc(gray)) (scatter coef time , msize(small) msymbol(circle) ), ///
	yli(0, lpattern(dash) lc(black)) ///
	ylab(-1.6(.2)0.6, labsize(small) angle(horizontal)) xlab(-10(2)10, labsize(small)) ///
	title("People with No Convictions") ///
	ytitle("") xtitle("") ///
	graphregion( fcolor(white) lcolor(white)) plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none)) ///
	legend(off) xsize(5) ysize(3) name(c, replace)
	

restore


**
* Violent counts
**

glm violentcount indcenteredN10-indcentered10 i.month i.statecounty  , family(nbinomial ml) ///
cluster(state) link(log) irls

preserve

* Get coef
regsave indcenteredN10-indcentered10, detail(all)

* Create variables
gen ub = coef + 1.96   * stderr  * sqrt(deviance/df)
gen lb = coef - 1.96 * stderr  * sqrt(deviance/df)


* Generate time variable and other graphing stuff
gen time = _n-11
replace time = time+1 if time > -1

twoway (rspike ub lb time , lw(0.3) lc(gray)) (scatter coef time , msize(small) msymbol(circle) ), ///
	yli(0, lpattern(dash) lc(black)) ///
	ylab(-1.6(.2)0.6, labsize(small) angle(horizontal)) xlab(-10(2)10, labsize(small)) ///
	title("People with Violent Convictions") ///
	ytitle("") xtitle("") ///
	graphregion( fcolor(white) lcolor(white)) plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none)) ///
	legend(off) xsize(5) ysize(3) name(d, replace)
	
restore

**
* Combined Figure
**

graph combine a b c d, xsize(3) ysize(2) ///
l1title("Change in Log Deportations Relative to Date of Policy", size(med)) ///
b1title("Months Since Sanctuary Policy")

graph export "s8.pdf", replace
